package com.hxd.mall.module.product.home

import android.annotation.SuppressLint
import android.graphics.Paint
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.RequestOptions.bitmapTransform
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hxd.mall.R
import com.hxd.mall.constant.HxdConstants
import com.hxd.mall.constant.HxdGlobalInfo.instance
import com.hxd.mall.module.product.home.bean.ListBean
import com.hxd.mall.utils.open.StringUtil
import com.hxd.mall.utils.open.StringUtil.setIconText
import com.hxd.mall.utils.open.StringUtil.setSize
import com.hxd.mall.utils.open.load
import com.hxd.mall.utils.open.loadRectangularImg
import com.hxd.mall.utils.open.setIconAndText
import com.hxd.mall.widget.CenterCropRoundCornerTransform
import org.jetbrains.anko.dip
import org.jetbrains.anko.displayMetrics

/**
 * @author zhaoyong
 * @description:
 * @date :2019-10-14 18:54
 */
class ProductListAdapter : BaseQuickAdapter<ListBean, BaseViewHolder>(R.layout.adapter_product_item_layout) {

    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder?, item: ListBean?) {
        helper?.run {
            val totalPrice = getView<TextView>(R.id.originPrice)
            totalPrice?.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
            totalPrice?.text = (if (item?.subChannel == "tmall") "天猫价" else "淘宝价") + item?.reservePrice.toString()
            setText(R.id.price, item?.discountPrice.toString())
            setSize(this.getView(R.id.price), item?.discountPrice.toString())
            setText(R.id.discountPrice, "${item?.couponInfo}")
            setText(R.id.recive, "${item?.estimatedIncome}")
            setText(R.id.tv_pay_num, "${item?.volume}人付款")

            setIconText(getView<TextView>(R.id.title), getView<ImageView>(R.id.iv_logo_icon), item?.subChannel ?: "", item?.title.toString())

            item?.couponInfo?.run {
                setGone(R.id.discountPrice, !(TextUtils.isEmpty(this) || "0.00" == this || "0" == this))
            }
            item?.estimatedIncome?.run {
                setGone(R.id.layout_recive, !(TextUtils.isEmpty(this) || "0.00" == this || "0" == this))
            }
            setGone(R.id.view_right_space, layoutPosition % 2 == 0)

            //设置图片大小
            val imageView = getView<ImageView>(R.id.productPic)!!
            val params = imageView.layoutParams
            val size = (imageView.context.displayMetrics.widthPixels - imageView.context.dip(36)) / 2
            params.width = size
            params.height = size
            imageView.layoutParams = params

            //图片加载
            val requestManager = Glide.with(mContext)
            imageView.setTag(imageView.id, requestManager)
            //设置圆角
            val placeholder = bitmapTransform(CenterCropRoundCornerTransform(15))
            requestManager.load(item?.pictUrl ?: "").apply(placeholder.placeholder(R.mipmap.product_item_icon).error(R.mipmap.product_item_icon)).into(imageView)
        }
    }

    override fun onViewRecycled(holder: BaseViewHolder) {
        super.onViewRecycled(holder)

        //每次复用时先断开之前的请求
        val imageView = holder.getView<ImageView>(R.id.productPic) ?: return
        val requestManager = imageView.getTag(imageView.id) as RequestManager
        requestManager.clear(imageView)
    }

}